package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.milk.context.BaseContext;
import com.milk.dto.ShoppingCarDTO;
import com.milk.dto.ShoppingCarDeleteDTO;
import com.milk.entity.Milktea;
import com.milk.entity.ShoppingCart;
import com.milk.exception.BaseException;
import com.milk.mapper.MilkteaMapper;
import com.milk.mapper.ShoppingCartMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milk.service.IShoppingCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCarService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private MilkteaMapper milkteaMapper;

    /**
     * 添加购物车
     * @param dto
     */
    public void add(ShoppingCarDTO dto) {
        Long userId = BaseContext.getCurrentId();

        //查询奶茶信息
        Milktea milktea = milkteaMapper.selectById(dto.getMilkTeaId());

        //根据用户id和奶茶id查询购物车
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("milktea_id",dto.getMilkTeaId());

//        ShoppingCart shoppingCart = shoppingCartMapper.selectById(dto.getId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(qw);
        if (shoppingCarts == null || shoppingCarts.isEmpty()){
            //购物车没有该奶茶信息
            insertShoppingCar(milktea,dto);
        }else {
            ShoppingCart shoppingCart =null;
            for (ShoppingCart cart : shoppingCarts) {
                if (Objects.equals(cart.getMilkteaFlavor(), dto.getMilkTeaFlavor())) {
                    cart.setNumber(cart.getNumber() + 1);
                    cart.setAmount(cart.getAmount() + judgePrice(dto.getCup(), milktea));
                    shoppingCart = new ShoppingCart();
                    BeanUtils.copyProperties(cart, shoppingCart);
//                    shoppingCartMapper.updateById(cart);
//                    break;
                }
//                }else {
//                    insertShoppingCar(milktea,dto);
//                }
            }
            if (shoppingCart == null){
                insertShoppingCar(milktea,dto);
            }else {
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
//        if (shoppingCart == null){
//            //购物车没有该奶茶信息
//           insertShoppingCar(milktea,dto);
//        }else {
//            if (Objects.equals(shoppingCart.getMilkteaFlavor(), dto.getMilkTeaFlavor())){
//                shoppingCart.setNumber(shoppingCart.getNumber()+1);
//                shoppingCart.setAmount(shoppingCart.getAmount()+judgePrice(dto.getCup(),milktea));
//                shoppingCartMapper.updateById(shoppingCart);
//                }else {
//                    insertShoppingCar(milktea,dto);
//                }
//        }
    }

    /**
     * 添加购物车实体
     * @param milktea
     * @param dto
     */
    public void insertShoppingCar(Milktea milktea , ShoppingCarDTO dto){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setName(milktea.getName());
        shoppingCart.setImage(milktea.getImage());
        shoppingCart.setMilkteaId(dto.getMilkTeaId());
        shoppingCart.setMilkteaFlavor(dto.getMilkTeaFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断规格
        shoppingCart.setAmount(dto.getNumber()*judgePrice(dto.getCup(),milktea));

        shoppingCartMapper.insert(shoppingCart);
    }
    /**
     * 判断规格
     * @param cup
     * @return
     */
    public Double judgePrice(Integer cup,Milktea milktea){
        if (cup == 1){
            return milktea.getPriceMin();
        }else if (cup == 2){
            return milktea.getPriceMedium();
        }else if (cup == 3){
            return milktea.getPriceMax();
        }
        throw new BaseException("添加错误");
    }

    /**
     * 减少一个购物车奶茶数量
     */
    public void deleteOne(Long id) {

//        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
//        qw.eq("user_id",BaseContext.getCurrentId());
//        qw.eq("milktea_id",dto.getMilkTeaId());
//        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(qw);
//        if (shoppingCarts == null){
//            //如果为空说明传值错误
//           throw new BaseException("删除失败");
//        }
//        for (ShoppingCart shoppingCart : shoppingCarts) {
//            if (Objects.equals(shoppingCart.getMilkteaFlavor(), dto.getMilkTeaFlavor())){
//                if (shoppingCart.getNumber() == 1){
//                    shoppingCartMapper.deleteById(shoppingCart);
//                }else {
//                    shoppingCart.setNumber(shoppingCart.getNumber()-1);
//                    shoppingCartMapper.updateById(shoppingCart);
//                }
//            }
//        }
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(id);
        if (shoppingCart == null){
            //如果为空说明传值错误
            throw new BaseException("删除失败");
        }
        if (shoppingCart.getNumber()==1){
            shoppingCartMapper.deleteById(id);
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateById(shoppingCart);
        }
    }

    /**
     * 批量删除
     * @param dto
     */
    public void deleteById(ShoppingCarDeleteDTO dto) {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("user_id",BaseContext.getCurrentId());
        qw.in("milktea_id",dto.getIds());

        shoppingCartMapper.delete(qw);
    }

    /**
     * 查询购物车
     * @return
     */
    public List<ShoppingCart> selectAll() {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("user_id",BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(qw);
    }

    /**
     * 清空购物车
     */
    public void deleteAll() {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq("user_id",BaseContext.getCurrentId());
        shoppingCartMapper.delete(qw);
    }
}
